package com.kadron.users;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KUserDAO;
import com.kadron.dao.KUserImageDAO;
import com.kadron.dao.KUserKadronsDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.entity.User;
import com.kadron.enumeration.UserStatus;
import com.kadron.manager.KSelection;
import com.kadron.service.UserHistoryService;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kUserListManager")
public class KUserListManager extends AbstractUserManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -978038977440805560L;
	
	private static final Log log = LogFactory.getLog(KUserListManager.class);
	
	private KDataModel userList;
	private KUserModelDataProvider dataProvider;
	
	@In
	private KUserDAO userDAO;
	@In
	private KUserImageDAO userImageDAO;
	@In
	private KUserKadronsDAO userKadronsDAO;
	@In
    private UserHistoryService userHistoryService;
	
	@Create
	public void init() {
		if (log.isDebugEnabled()) {
			log.debug("Init "+this.getClass().getName());
		}
		dataProvider = new KUserModelDataProvider(new KUserModelCreator(userDAO, userImageDAO, userKadronsDAO), userDAO);
		userList = new KDataModel(dataProvider);
	}

	public KDataModel getUserList() {
		return userList;
	}
	
	public String edit(Long id) {
		if (id == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(id);
		return "e_user";
	}
	
	public void activate(User user) {
		if (user == null) {
			bad(KConst.BAD_THING);
			return;
		}
		UserStatus oldStatus = UserStatus.valueOf(user.getUserStatus().toString());
		user.setUserStatus(UserStatus.CONFIRMED);
		user = (User) userDAO.update(user);
		userHistoryService.create(user, "User status update by admin", "USER_STATUS", "User K-NAME="+user.getUsername()+" changed from userStatus="+oldStatus+", into UserStatus="+user.getUserStatus());
	}
	
	public void delete(User user) {
		if (user == null) {
			bad(KConst.BAD_THING);
			return;
		}
		user.setUserStatus(UserStatus.DELETED);
		user = (User) userDAO.update(user);
		userHistoryService.create(user, "User status update by admin", "USER_STATUS", "User K-NAME="+user.getUsername()+" changed UserStatus="+user.getUserStatus());
	}
	
	public void bann(User user) {
		if (user == null) {
			bad(KConst.BAD_THING);
			return;
		}
		user.setUserStatus(UserStatus.BANNED);
		user = (User) userDAO.update(user);
		userHistoryService.create(user, "User status update by admin", "USER_STATUS", "User K-NAME="+user.getUsername()+" changed UserStatus="+user.getUserStatus());
	}
	
}